package com.project.util;

import lombok.extern.slf4j.Slf4j;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * 日期计算工具类
 *
 * @author Leo高洋
 * @create 2023-06-03 19:38
 */
@Slf4j
public class DateCalculation {
    private static SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
    private static Calendar startDate = Calendar.getInstance();
    private static Calendar endDate = Calendar.getInstance();
    private static Date earlydate = new Date();
    private static Date latedate = new Date();

    /**
     * 计算两个时间相差多少个年
     */
    public static int yearsBetween(String start, String end) throws ParseException {
        startDate.setTime(sdf.parse(start));
        endDate.setTime(sdf.parse(end));
        int years = endDate.get(Calendar.YEAR) - startDate.get(Calendar.YEAR);
        if (years < 1) {
            years = years + 1;
        }
        return years;
    }

    /**
     * 计算两个时间相差多少个月
     */
    public static int monthsBetween(String start, String end) throws ParseException {
        startDate.setTime(sdf.parse(start));
        endDate.setTime(sdf.parse(end));
        int result = yearsBetween(start, end) * 12 + endDate.get(Calendar.MONTH) - startDate.get(Calendar.MONTH);
        return result == 0 ? 1 : Math.abs(result);

    }

    /**
     * 计算两个时间相差多少个天
     */
    public static int daysBetween(String start, String end) throws ParseException {
        // 得到两个日期相差多少天
        return hoursBetween(start, end) / 24;
    }

    /**
     * 计算两个时间相差多少小时
     */
    public static int hoursBetween(String start, String end) throws ParseException {
        // 得到两个日期相差多少小时
        return minutesBetween(start, end) / 60;
    }

    /**
     * 计算两个时间相差多少分
     */
    public static int minutesBetween(String start, String end) throws ParseException {
        // 得到两个日期相差多少分
        return secondesBetween(start, end) / 60;
    }

    /**
     * 计算两个时间相差多少秒
     */
    public static int secondesBetween(String start, String end) throws ParseException {
        log.info("==========start:" + start + ",end:" + end);
        earlydate = sdf.parse(start);
        latedate = sdf.parse(end);
        startDate.setTime(earlydate);
        endDate.setTime(latedate);
        // 设置时间为 0 时
        startDate.set(Calendar.HOUR_OF_DAY, 0);
        startDate.set(Calendar.MINUTE, 0);
        startDate.set(Calendar.SECOND, 0);
        endDate.set(Calendar.HOUR_OF_DAY, 0);
        endDate.set(Calendar.MINUTE, 0);
        endDate.set(Calendar.SECOND, 0);
        // 得到两个日期相差多少秒
        return ((int) (endDate.getTime().getTime() / 1000) - (int) (startDate.getTime().getTime() / 1000));
    }

    public static void main(String[] args) throws ParseException {
        String todayStr = sdf.format(new Date());
        System.out.println("两个日期之间的年数字：" + yearsBetween("1990-01-07", todayStr));
        System.out.println("两个日期之间的月数字：" + monthsBetween("1990-01-07", todayStr));
        System.out.println("两个日期之间的天数字：" + daysBetween("1990-01-07", todayStr));
    }
}